﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//Thu Vien Them Vao
using QuanLyQuanCafe.BusinessLogic;
using QuanLyQuanCafe.BusinessObject;
namespace QuanLyQuanCafe
{
    public partial class FrmKhachHang : Form
    {
        KhachHangBL khachhangBL = new KhachHangBL();        
        public FrmKhachHang()
        {
            InitializeComponent();
            //Them DU Lieu Vao ComboBox Gioi Tinh
            cboGioiTinh.Items.Add("Nam");
            cboGioiTinh.Items.Add("Nữ");
            //Them DU Lieu Vao ComboBox Nam Sinh
            for (int i = DateTime.Now.Year - 55; i <= DateTime.Now.Year - 18; i++)
            {
                cboNamSinh.Items.Add(i);
            }
        }

        private void FrmKhachHang_Load(object sender, EventArgs e)
        {
            dataGridViewKhachHang.DataSource = khachhangBL.LayDanhSachKhachHang();
            AnHienButton(false);
            btnXoa.Enabled = false;
        }
        private void CapNhatText()
        {
            txtHoTen.Text = "";
            txtDiaChi.Text = "";
            txtNgheNghiep.Text = "";
            txtSoThich.Text = "";
            txtEmail.Text = "";
            txtDienThoai.Text = "";
            cboGioiTinh.Text = "Nam";
            cboNamSinh.Text = Convert.ToString(DateTime.Now.Year - 20);            
        }

        private void AnHienButton(bool flag)
        {
            btnThem.Enabled = !flag;
            btnSua.Enabled = !flag;
            //btnXoa.Enabled = !flag;
            btnLuu.Enabled = flag;
            btnHuy.Enabled = flag;

            txtCMND.Enabled = flag;
            txtHoTen.Enabled = flag;            
            txtDiaChi.Enabled = flag;
            txtNgheNghiep.Enabled = flag;
            txtSoThich.Enabled = flag;
            txtEmail.Enabled = flag;
            cboGioiTinh.Enabled = flag;            
            txtDienThoai.Enabled = flag;
            cboNamSinh.Enabled = flag;

            dataGridViewKhachHang.Enabled = !flag;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            CapNhatText();
            txtCMND.Text = "";
        }

        private KhachHangBO LayThongTinKhachHang()
        {
            KhachHangBO khachhangBO = new KhachHangBO();
            khachhangBO.CMND_KH = txtCMND.Text;
            khachhangBO.HoTen = txtHoTen.Text;
            khachhangBO.NamSinh = int.Parse(cboNamSinh.Text);
            khachhangBO.DiaChi = txtDiaChi.Text;
            khachhangBO.GioiTinh = cboGioiTinh.Text;
            khachhangBO.Email = txtEmail.Text;
            khachhangBO.NgheNghiep = txtNgheNghiep.Text;
            khachhangBO.SoThich = txtSoThich.Text;
            khachhangBO.SoDT = txtDienThoai.Text;

            return khachhangBO;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            AnHienButton(false);
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            try
            {
                KhachHangBO khachhangBO = LayThongTinKhachHang();
                if (khachhangBL.KiemTraCMNDTonTai(khachhangBO.CMND_KH))
                {
                    if (khachhangBL.SuaKhachHang(khachhangBO))
                        FrmKhachHang_Load(sender, e);
                }
                else
                {
                    if (khachhangBL.ThemKhachHang(khachhangBO))
                        FrmKhachHang_Load(sender, e);
                }
            }
            catch
            {
                MessageBox.Show("Kiểm Lại Các Thông Tin Đã Nhập.", "Quán Lý Quán Cafe", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            AnHienButton(true);
            txtCMND.Enabled = false;
        }

        private void dataGridViewKhachHang_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            try
            {
                txtCMND.Text = dataGridViewKhachHang.Rows[row].Cells["CMND_KH"].Value.ToString();
                txtHoTen.Text = dataGridViewKhachHang.Rows[row].Cells["HoTen"].Value.ToString();
                cboNamSinh.Text = dataGridViewKhachHang.Rows[row].Cells["NamSinh"].Value.ToString();
                txtDiaChi.Text = dataGridViewKhachHang.Rows[row].Cells["DiaChi"].Value.ToString();
                txtEmail.Text = dataGridViewKhachHang.Rows[row].Cells["Email"].Value.ToString();
                txtDienThoai.Text = dataGridViewKhachHang.Rows[row].Cells["SoDT"].Value.ToString();
                cboGioiTinh.Text = dataGridViewKhachHang.Rows[row].Cells["GioiTinh"].Value.ToString();
                txtNgheNghiep.Text = dataGridViewKhachHang.Rows[row].Cells["NgheNghiep"].Value.ToString();
                txtSoThich.Text = dataGridViewKhachHang.Rows[row].Cells["SoThich"].Value.ToString();
            }
            catch
            {
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Khách Hàng [" + txtHoTen.Text + "] Ra Khỏi Danh Sách Khách Hàng Thân Thiết Không?", "Quản Lý Quán Cafe", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                if (khachhangBL.XoaKhachHang(txtCMND.Text))
                    FrmKhachHang_Load(sender, e);
        }
    }
}
